#include "MatlabInterface.h"

#if defined(MATLAB_PLOT)
	#pragma comment( lib, "libmx"  )
	#pragma comment( lib, "libmex"  )
	#pragma comment( lib, "libmat"  )
	#pragma comment( lib, "libeng"  )
#endif

namespace matlab{

Engine* MatlabInterface::ep; 
int MatlabInterface::instances = 0; 


MatlabInterface::MatlabInterface(void)
{
#ifdef MATLAB_PLOT
	//start matlab engine if not already started
	if (instances ==0 && !ep)
	{
//		if (!(ep = engOpen("\0"))) 
		if (!(ep = engOpen("ssh nouri@taft \"export DISPLAY=dice.rutgers.edu:0 ; matlab;\"")))
		{
			printf( "\nCan't start MATLAB engine\n");
			return; 
		}
	}
	instances++; 
#endif
}

MatlabInterface::~MatlabInterface(void)
{
#ifdef MATLAB_PLOT
	instances--; 
	if (instances==0 && ep)
	{
		engClose(ep);
	}
#endif
}
}
